$path = ".\tests"
$launch=1
foreach ($arg in $Args)
{ 
    if ($arg -eq "-clean")
    {
        $launch=0
    }
	else
	{
		write-host "usage: launch_test [-clean]" -foregroundcolor red
		exit
	}
}

rm -Force "$path\*.c" 2> $null
rm -Force "$path\*.h" 2> $null
rm -Force "$path\*.out"  2> $null
rm -Force "$path\*.ktree"  2> $null
Write-Host "Tests cleaned" -foregroundcolor green
if ($launch -eq 1)
{
	## TEST .c ##
	Write-Host "TEST *.c" -foregroundcolor green
	$i=1
    while (test-path "$path\test$i.kc")
    {
		.\kooc.ps1 "$path\test$i.kc" > "$path\test$i.out" 2>&1
		if (test-path "$path\test$i.c")
		{
			if (test-path "$path\correction\test$i.cor")
			{
				$fileDif = @(Get-Content "$path\test$i.c")
				$fileRef = @(Get-Content "$path\correction\test$i.cor")
				$res = @(compare-object -referenceobject $fileRef -differenceobject $fileDif) 2>&1
				if (!$res)
				{
					Write-Host "test$i PASSED" -foregroundcolor green
				}
				else
				{
					$res >> "$path\test$i.out"
					Write-Host "test$i FAILED" -foregroundcolor red
					cat "$path\test$i.out"
				}
			}
			else
			{
				Write-Host "test$i.cor IS MISSING" -foregroundcolor red
			}
		}
		else
		{
			Write-Host "test$i KOOC error" -foregroundcolor red
			cat "$path\test$i.out"
		}
        $i++
    }
	
	##TEST .h ##
	Write-Host "TEST *.h" -foregroundcolor green
	$i=1
    while (test-path "$path\test$i.kh")
    {
		.\kooc.ps1 "$path\test$i.kh" > "$path\test$i.out" 2>&1
		if (test-path "$path\test$i.h")
		{
			if (test-path "$path\correction\test$i.h")
			{
				$fileDif = @(Get-Content "$path\test$i.h")
				$fileRef = @(Get-Content "$path\correction\test$i.h")
				$res = @(compare-object -referenceobject $fileRef -differenceobject $fileDif) 2>&1
				if (!$res)
				{
					Write-Host "test$i PASSED" -foregroundcolor green
				}
				else
				{
					$res >> "$path\test$i.out"
					Write-Host "test$i FAILED" -foregroundcolor red
					cat "$path\test$i.out"
				}
			}
			else
			{
				Write-Host "test$i.h IS MISSING" -foregroundcolor red
			}
		}
		else
		{
			Write-Host "test$i KOOC error" -foregroundcolor red
			cat "$path\test$i.out"
		}
        $i++
    }
}
